package com.anysoft.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.anysoft.R;
import com.anysoft.base.BaseActivity;
import com.anysoft.bean.UserBean;
import com.anysoft.utils.ParamUtil;
import com.orhanobut.logger.Logger;

import java.util.List;

import static com.anysoft.utils.BitmapUtils.getDiskBitmap;

public class UserManageListviewAdapter extends BaseAdapter {

    private Context mContext;
    private List<UserBean> itemList;
    private LayoutInflater inflater;

    public UserManageListviewAdapter(Context context, List<UserBean> itemList) {
        this.itemList = itemList;
        this.mContext = context;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return itemList.size();
    }

    @Override
    public UserBean getItem(int position) {
        return itemList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        UserManageListViewHolder holdler;

        Bitmap defaultBitmap = drawableToBitmap(mContext.getResources().getDrawable(R.mipmap.icon_user));

        if (convertView == null) {
            holdler = new UserManageListViewHolder();
            convertView = inflater.inflate(R.layout.listview_item_user_manage, null);

            holdler.iv_item_user_manage = (ImageView) convertView
                    .findViewById(R.id.iv_item_user_manage);
            holdler.iv_item_user_manage_selected = (ImageView) convertView
                    .findViewById(R.id.iv_item_user_manage_selected);
            holdler.tv_item_user_manage_name = (TextView) convertView
                    .findViewById(R.id.tv_item_user_manage_name);

            convertView.setTag(holdler);
        } else {
            holdler = (UserManageListViewHolder) convertView.getTag();
            resetViewHolder(holdler);
        }

        String pathUrl = itemList.get(position).getUserImgPath();
        if (!TextUtils.isEmpty(pathUrl)) {
            Logger.t("pathUrl").d(pathUrl);
            Bitmap bitmap = getDiskBitmap(pathUrl);
            if (bitmap != null) {
                holdler.iv_item_user_manage.setImageBitmap(bitmap);
            } else {
                holdler.iv_item_user_manage.setImageBitmap(defaultBitmap);
            }
        } else {
            if("0".equals(itemList.get(position).getSex())){//女
                defaultBitmap = drawableToBitmap(mContext.getResources().getDrawable(R.mipmap.icon_user_w));
            }else if("1".equals(itemList.get(position).getSex())){//男
                defaultBitmap = drawableToBitmap(mContext.getResources().getDrawable(R.mipmap.icon_user_m));
            }
            holdler.iv_item_user_manage.setImageBitmap(defaultBitmap);
//            holdler.iv_item_user_manage.setBackground(mContext.getResources().getDrawable(R.mipmap.icon_user));
        }

        holdler.tv_item_user_manage_name.setText(ParamUtil.formatUserName(itemList.get(position).getUserName()));
        if (!((BaseActivity) mContext).mentorApplication.needLogin()) {
            if (((BaseActivity) mContext).mentorApplication.userBean.getUserId().equals(itemList.get(position).getUserId())) {
                holdler.iv_item_user_manage_selected.setVisibility(View.VISIBLE);
            } else {
                holdler.iv_item_user_manage_selected.setVisibility(View.INVISIBLE);
            }
        } else {
            holdler.iv_item_user_manage_selected.setVisibility(View.INVISIBLE);
        }

        return convertView;
    }

    private void resetViewHolder(UserManageListViewHolder holdler) {
        holdler.iv_item_user_manage.setImageBitmap(drawableToBitmap(mContext.getResources().getDrawable(R.mipmap.icon_user)));
//        holdler.iv_item_user_manage.setBackground(mContext.getResources().getDrawable(R.mipmap.icon_user));
        holdler.iv_item_user_manage_selected.setVisibility(View.INVISIBLE);
    }

    public static Bitmap drawableToBitmap(Drawable drawable) {
        int width = drawable.getIntrinsicWidth();
        int height = drawable.getIntrinsicHeight();
        Bitmap bitmap = Bitmap.createBitmap(width, height, drawable
                .getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
                : Bitmap.Config.RGB_565);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, width, height);
        drawable.draw(canvas);
        return bitmap;
    }
}